<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
        <el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
            <el-form-item label="问题" prop="message">
                <el-input v-model="form.message" clearable></el-input>
            </el-form-item>
            <el-form-item label="是否有效" prop="phone">
                <el-input v-model="form.phone" clearable></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible=false" >取 消</el-button>
            <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
    export default {
        emits: ['success', 'closed'],
        data() {
            return {
                mode: "add",
                titleMap: {
                    add: '新增反馈',
                    edit: '编辑反馈'
                },
                form: {
                    id:"",
                    message: "",
                    phone: ""
                },
                rules: {
                    message:[
                        {required: true, message: '反馈信息必填'}
                    ],
                    phone:[
                        {required: true, message: '联系方式必填'}
                    ],
                },
                visible: false,
                isSaveing: false
            }
        },
        methods: {
            //显示
            open(mode='add'){
                this.mode = mode;
                this.visible = true;
                return this;
            },
            //表单提交方法
            submit(){
                this.$refs.dialogForm.validate(async (valid) => {
                    if (valid) {
                        this.isSaveing = true;
                        var res = [];
                        if(this.mode=='add'){
                            res = await this.$API.system.message.create.post(this.form);
                        }else{
                            res = await this.$API.system.message.update.post(this.form);
                        }
                        this.isSaveing = false;
                        if(res.code == 0){
                            this.$emit('success', this.form, this.mode)
                            this.visible = false;
                            this.$message.success("操作成功")
                        }else{
                            this.$alert(res.message, "提示", {type: 'error'})
                        }
                    }
                })
            },
            //表单注入数据
            setData(data){
                this.form.id = data.id
                this.form.ip = data.ip
                this.form.message = data.message
                this.form.phone = data.phone
            }
        }
    }
</script>

<style>
</style>
